﻿///***************************************************************
/// 시  스  템 : Updms.Mny
/// 단위시스템 : MNY
/// 계      층 : Presentation Layer
/// 프로그램명 : Mny2016r
/// 파  일  명 : Mny2016r.aspx.cs
/// 설      명 : 연간출납내역
///***************************************************************
/// 수정ID 일자       이름     내용
///---------------------------------------------------------------
/// CH001  2008-10-23 송시명   최초 작성
///***************************************************************
using System;
using System.Data;
using System.Collections;
using System.Web.UI;
using System.Web.UI.WebControls;

using SMFramework.Data;
using SMFramework.Utility;
using SMFramework.Web.UI.WebControls.SMGridView;

using Base.Dac;
using Updms.Mny.Exe;


namespace Updms.Mny
{
	public partial class Mny2016r : Updms.Common.BaseClass.PageBase
	{
		#region 전역영역

		//프로그램상수
		public static readonly string PID = "Mny2016r";
		public static readonly string gs_path_info = "/updms/Mny/" + PID + ".aspx";

		Global global = new Global();
		CommCode.Mny mny = new CommCode.Mny(LANG_SET);

		//검색
		protected string kw1 = "";	//기준년도
		protected string kw2 = "";	//구분
		protected string kw3 = "";	//계정
		protected string kw4 = "";	//내용

		//메뉴 선택값
		protected string gs_sm = "";

		// 페이지모드
		private string gs_page_mode = "";

		#endregion

		#region Page_Load, OnPageLoad

		protected void Page_Load(object sender, EventArgs e)
		{
			SetInit();
			if (!Page.IsPostBack)
			{
				// 최초 화면 로딩시 처리해야 할 부분
				OnPageLoad();
				SetEnabledYearBtn();
			}
		}

		// OnPageLoad
		private void OnPageLoad()
		{
			SetLiterals(global.GetLabelInfoByPgmId(PID, LANG_SET));
			SetClientScript();
			SetBaseYear();
			SetClass();
			SetSearchAccount();
			SetSearchParameter();
		}

		#endregion

		#region InitializeComponent

		private void InitializeComponent()
		{
			this.Load += new System.EventHandler(this.Page_Load);
			this.PreRender += new System.EventHandler(this.Page_PreRender);
		}

		#endregion

		#region SetClientScript

		private void SetClientScript()
		{
			//엔터검색
			txt_search_contents.Attributes.Add("onkeypress", "enterSearch(event)");
		}

		#endregion

		#region SetInit, SetSearchParameter

		//초기값설정
		private void SetInit()
		{
			lbl_search_flag.Text = "INIT";

			gs_sm = (Request.QueryString["sm"] == null || Request.QueryString["sm"].Trim().Length != 7)
					? global.GetDirDepth(PID) : Request.QueryString["sm"].ToString();
		}

		//검색파라미터
		private void SetSearchParameter()
		{
			switch (lbl_search_flag.Text.Trim().ToUpper())
			{
				case "INIT":
					if (String.IsNullOrEmpty(Request.QueryString["kw1"]))	// url에 값이 없으면
					{
						kw1 = ddl_search_base_year.SelectedValue;
					}
					else
					{
						kw1 = Server.UrlDecode(Request.QueryString["kw1"]);
						ddl_search_base_year.SelectedValue = kw1;
					}
					
					if (String.IsNullOrEmpty(Request.QueryString["kw2"]))	// url에 값이 없으면
					{
						kw2 = ddl_search_class.SelectedValue;
					}
					else
					{
						kw2 = Server.UrlDecode(Request.QueryString["kw2"]);
						ddl_search_class.SelectedValue = kw2;
					}
					SetSearchAccount();

					if (String.IsNullOrEmpty(Request.QueryString["kw3"]))	// url에 값이 없으면
					{
						kw3 = ddl_search_account.SelectedValue;
					}
					else
					{
						kw3 = Server.UrlDecode(Request.QueryString["kw3"]);
						ddl_search_account.SelectedValue = kw3;
					}

					if (String.IsNullOrEmpty(Request.QueryString["kw4"]))	// url에 값이 없으면
					{
						kw4 = txt_search_contents.Text.Trim();
					}
					else
					{
						kw4 = Server.UrlDecode(Request.QueryString["kw4"]);
						txt_search_contents.Text = kw4;
					}
					break;
				case "SEARCH":
					kw1 = ddl_search_base_year.SelectedValue;
					kw2 = ddl_search_class.SelectedValue;
					kw3 = ddl_search_account.SelectedValue;
					kw4 = txt_search_contents.Text.Trim();
					break;
			}
		}


		#endregion

		#region 마스터 Method (SetBaseYear, SetClass, SetSearchAccount)

		//기준년도 설정
		private void SetBaseYear()
		{
			ddl_search_base_year.Items.Clear();

			int li_f_year = Convert.ToInt32(SEARCH_Y_MIN);
			int li_t_year = Convert.ToInt32(DateUtil.GetYearEx(0, "yyyy"));
			for (int i = li_f_year; i <= li_t_year; i++)
			{
				string ls_year = i.ToString();
				ddl_search_base_year.Items.Add(new ListItem(ls_year, ls_year));
			}

			ddl_search_base_year.SelectedValue = li_t_year.ToString(); // 기본값은 현재년도
		}

		//구분
		private void SetClass()
		{
			mny.SetClass(ddl_search_class);
			ddl_search_class.Items.Insert(0, new ListItem(DDL_SELECT, ""));
		}

		//계정
		private void SetSearchAccount()
		{
			string ls_ucd = "";
			DataSet lo_ds = new DataSet();
			ddl_search_account.Items.Clear();

			if (ddl_search_class.SelectedIndex == 0)
			{
				ddl_search_account.Items.Insert(0, new ListItem(DDL_SELECT, ""));
			}
			else
			{
				ls_ucd = ddl_search_class.SelectedValue;
				lo_ds.Merge(global.GetCommUCd("MNY", ls_ucd, LANG_SET, DDL_SELECT));

				ddl_search_account.DataSource = lo_ds;
				ddl_search_account.DataMember = "UPDMS_COM_CD";
				ddl_search_account.DataTextField = "Text";
				ddl_search_account.DataValueField = "Value";
				ddl_search_account.DataBind();
			}
		}

		protected void ddl_search_class_SelectedIndexChanged(object sender, EventArgs e)
		{
			SetSearchAccount();
		}

		#endregion

		#region 마스터 Method (SetPrevNextYear,SetThisYear, SetEnabledYearBtn)

		//전년, 차년버튼 누르면
		private void SetPrevNextYear(int pi_year_cnt)
		{
			string ls_year = ddl_search_base_year.SelectedValue;
			string ls_calc_year = Convert.ToString(Convert.ToInt32(ls_year) + pi_year_cnt);

			if (WebControlUtil.CheckDdlValue(ddl_search_base_year, ls_calc_year))
			{
				ddl_search_base_year.SelectedValue = ls_calc_year;
			}

			//버튼처리
			SetEnabledYearBtn();
		}

		//당년설정
		private void SetThisYear()
		{
			string ls_this_year = DateUtil.GetNow("yyyy");
			ddl_search_base_year.SelectedValue = ls_this_year;
			SetEnabledYearBtn();
		}

		//전년, 차년버튼 처리
		private void SetEnabledYearBtn()
		{
			ibtn_prev_year.Enabled = (ddl_search_base_year.SelectedIndex == 0) ? false : true;
			ibtn_next_year.Enabled = (ddl_search_base_year.SelectedIndex == ddl_search_base_year.Items.Count - 1) ? false : true;
		}

		#endregion

		#region 디테일 Method ()

		#endregion

		#region 리스트 (SelectProcess, SearchProcess)

		//조회
		private void SelectProcess()
		{
			DataSet lo_ds = new DataSet();
			DataPack lo_dp = new DataPack();

			//검색
			lo_dp.AddProperty("ls_base_year", typeof(string), kw1);
			lo_dp.AddProperty("ls_class", typeof(string), kw2);
			lo_dp.AddProperty("ls_account", typeof(string), kw3);
			lo_dp.AddProperty("ls_contents", typeof(string), kw4);
			lo_dp.AddProperty("ls_lang_set", typeof(string), LANG_SET);

			using (Mny2016r_NT _Mny2016r_NT = new Mny2016r_NT())
			{
				lo_ds.Merge(_Mny2016r_NT.GetYearlyMoneyInOutList(lo_dp));
			}

			if (!DataSetUtil.CheckRowCount(lo_ds, "UPDMS_MNY_REPORT"))
			{
				CallMsgBoxInAjax("MSG_ERR_111");	// 등록된 내용이 없습니다
			}

			GridView1.DataSource = lo_ds;
			GridView1.DataBind();
		}

		//검색
		private void SearchProcess()
		{
			gs_page_mode = "R";
			lbl_search_flag.Text = "SEARCH";
			SetSearchParameter();

			SelectProcess();
		}

		#endregion

		#region Page_PreRender, ChangePageMode

		// Page_PreRender
		private void Page_PreRender(object sender, System.EventArgs e)
		{
			if (!Page.IsPostBack)
			{
				using (AccessCheck access = new AccessCheck())
				{
					//접근권한 체크
					if (!access.CheckPgmAccessGrant(PID, USER_ID, ACCESS_LOG_INFO))
					{
						Response.Redirect(URL_NO_AUTHORITY_REDIRECT_BASE);	//사용자의 권한은 요청하신 페이지를 볼 수 없습니다
					}
				}
			}
			ChangePageMode();
		}

		// ChangePageMode
		private void ChangePageMode()
		{
			switch (gs_page_mode)
			{
				case "":	// 처음 페이지로드시
					table_list_set.Visible = false;
					break;
				case "R":	// 검색버튼 클릭시
					table_list_set.Visible = true;
					break;
			}
			SetGridViewHeader(global.GetColumnListByPgmId(PID, LANG_SET));
		}

		#endregion

		#region GridView이벤트

		//RowDataBound
		protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
		{
			//헤더의 rowspan되는 Cell을 삭제함
			if (e.Row.RowType == DataControlRowType.Header)
			{
				e.Row.Cells[0].Visible = false;
			}
			
			if (e.Row.RowType == DataControlRowType.DataRow)
			{
				//마우스올렸을 때
				e.Row.Attributes.Add("onMouseOver", GRID_ONMOUSEOVER_COLOR);
				e.Row.Attributes.Add("onMouseOut", GRID_ONMOUSEOUT_COLOR);

				//셀바탕색
				if (rbl_weekend_div.SelectedValue == "Y")
				{
					for (int i = 0; i < e.Row.Cells.Count; i++)
					{
						if (e.Row.Cells[i].Text.Length > 37)
						{
							string ls_day_of_week = e.Row.Cells[i].Text.Substring(27, 3);
							string ls_holiday_yn = e.Row.Cells[i].Text.Substring(30, 1);

							//주말처리
							if (ls_day_of_week == "Sat")
							{
								e.Row.Cells[i].Attributes.Add("class", "bg_bluesky");
							}
							else if (ls_day_of_week == "Sun")
							{
								e.Row.Cells[i].Attributes.Add("class", "bg_pink");
							}

							//휴일처리
							if (ls_holiday_yn == "1")
							{
								e.Row.Cells[i].Attributes.Add("class", "bg_pink");
							}
						}
					}
				}
			}
			
			//멀티헤더설정
			SortedList lo_sl = new SortedList();
			SMGridView sGv = new SMGridView();

			string dt = global.GetLabelNmBySeq(377, LANG_SET) + ",1,2"; //일
			string first_quarter = global.GetLabelNmBySeq(5, LANG_SET) + ",3,1"; //1분기
			string second_quarter = global.GetLabelNmBySeq(9, LANG_SET) + ",3,1"; //2분기
			string third_quarter = global.GetLabelNmBySeq(14, LANG_SET) + ",3,1"; //3분기
			string fourth_quarter = global.GetLabelNmBySeq(16, LANG_SET) + ",3,1"; //4분기

			lo_sl.Add("1", dt);
			lo_sl.Add("2", first_quarter);
			lo_sl.Add("3", second_quarter);
			lo_sl.Add("4", third_quarter);
			lo_sl.Add("5", fourth_quarter);
			sGv.SetMultiHeader(e, lo_sl, "multi_header");
		}

		#endregion

		#region 버튼이벤트

		//전년
		protected void ibtn_prev_year_Click(object sender, EventArgs e)
		{
			SetPrevNextYear(-1);
			SearchProcess();
		}

		//당년
		protected void ibtn_this_year_Click(object sender, EventArgs e)
		{
			SetThisYear();
			SearchProcess();
		}

		//차년
		protected void ibtn_next_year_Click(object sender, EventArgs e)
		{
			SetPrevNextYear(1);
			SearchProcess();
		}

		//검색
		protected void ibtn_search_Click(object sender, EventArgs e)
		{
			SetEnabledYearBtn();
			SearchProcess();
		}

		//엑셀
		protected void ibtn_excel_Click(object sender, EventArgs e)
		{
			SetSearchParameter();

			DataSet lo_ds = new DataSet();
			DataPack lo_dp = new DataPack();

			lo_dp.AddProperty("ls_base_year", typeof(string), kw1);
			lo_dp.AddProperty("ls_class", typeof(string), kw2);
			lo_dp.AddProperty("ls_account", typeof(string), kw3);
			lo_dp.AddProperty("ls_contents", typeof(string), kw4);
			lo_dp.AddProperty("ls_lang_set", typeof(string), LANG_SET);

			using (Mny2016r_NT _Mny2016r_NT = new Mny2016r_NT())
			{
				lo_ds.Merge(_Mny2016r_NT.GetYearlyMoneyInOutList(lo_dp));
			}
			string fileName = PID + "(" + global.GetMenuTitle(PID, LANG_SET) + ")";
			ExcelUtil.CreateExcelFileX(this.Page, fileName, lo_ds, SetExcelHeaderX(global.GetColumnListByPgmId(PID, LANG_SET)));
		}

		#endregion

	}
}
